package com.conan.dailyflickr.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.conan.dailyflickr.R;
import com.conan.dailyflickr.app.App;

public class DailyFlickrWidgetSettingsActivity extends Activity {
	
	App mApp = null;
	
	LinearLayout mLayouPopupSetting;
	Button mBtnDone;
	CheckBox mCbExpanseAutoSetting;
	FrameLayout mRootViewOfAutoSetting;
	RadioButton mRbUpdatePeriod1h;
	RadioButton mRbUpdatePeriod2h;
	RadioButton mRbUpdatePeriod4h;
	
	private Animation animPushUpIn;
	private Animation animPushUpOut;
	
	private final int HANDLE_HIDE_VIEW_OF_AUTO_STG 	= 1000;
	
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch(msg.what) {
			case HANDLE_HIDE_VIEW_OF_AUTO_STG:
				if(mRootViewOfAutoSetting != null)
					mRootViewOfAutoSetting.setVisibility(View.GONE);
				break;
			}
		};
	};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        mApp = new App(DailyFlickrWidgetSettingsActivity.this);
//
//        ((DailyFlickrApplication) getApplication()).setActiveContext(
//				getClass().getCanonicalName(), 
//				this);

        // Have the system blur any windows behind this one.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

		setContentView(R.layout.flickr_widget_setting_layout);
		
		mBtnDone = (Button) findViewById(R.id.bt_flickr_setting_done_id);
		mBtnDone.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		mLayouPopupSetting = (LinearLayout) findViewById(R.id.layout_popup_id);
		
		mRootViewOfAutoSetting = (FrameLayout) findViewById(R.id.root_view4update_period_id);
		
		animPushUpIn = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_up_in);
		animPushUpOut = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_up_out);
		animPushUpOut.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) { }
			public void onAnimationRepeat(Animation animation) { }
			@Override
			public void onAnimationEnd(Animation animation) {
				mHandler.sendEmptyMessage(HANDLE_HIDE_VIEW_OF_AUTO_STG);
			}
		});
		mCbExpanseAutoSetting = (CheckBox) findViewById(R.id.checkbox_expanse_autosetting_id);
		mCbExpanseAutoSetting.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					mRootViewOfAutoSetting.setVisibility(View.VISIBLE);
					mLayouPopupSetting.startAnimation(animPushUpIn);
					mRootViewOfAutoSetting.startAnimation(animPushUpIn);
				} else {
					mRootViewOfAutoSetting.startAnimation(animPushUpOut);
					mLayouPopupSetting.startAnimation(animPushUpOut);
				}
			}
		});
		
		mRbUpdatePeriod1h =(RadioButton) findViewById(R.id.radiobutton_stg_auto_1h_id);
		mRbUpdatePeriod2h =(RadioButton) findViewById(R.id.radiobutton_stg_auto_2h_id);
		mRbUpdatePeriod4h =(RadioButton) findViewById(R.id.radiobutton_stg_auto_4h_id);
		
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	
    	overridePendingTransition(0, 0);
//    	// ...
//    	if (mApp.isPaused()) {
//    		// do something useful in here
//    		// BLAH BLAH...
//    		mApp.onResume();
//    	}
    }
    
    @Override
    protected void onPause() {
    	super.onPause();
    	overridePendingTransition(0, 0);
//    	// ...
//    	mApp.onPause();
    }
    
}
